package com.aura.bookshop.controller;

import com.aura.bookshop.entity.Book;
import com.aura.bookshop.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @RequestMapping("/queryAll")
    public String queryAll(Map<String,Object> map){
        List<Book> books = bookService.queryAll();
        map.put("books",books);
        return "main";
    }

    @RequestMapping("/delete/{bid}")
    public String delete(@PathVariable int bid,Map<String,Object> map){
        int delete = bookService.delete(bid);
        map.put("msg",delete);
        return "forward:/book/queryAll";
    }

    @RequestMapping("/add")
    public String add(Map<String,Object> map){
        return "add";
    }

    @RequestMapping("/add_do")
    public String add_do(Book book,Map<String,Object> map){
        int add = bookService.add(book);
        map.put("msg",add);
        if(add > 0){
            return "forward:/book/queryAll";
        }else{
            return "forward:/book/add";
        }

    }

}
